package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.Login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"all"})
public class UserInfoApiController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 根据用户id获取用户信息
     *
     * @param userId
     * @return
     */
    @Operation(summary = "根据用户id获取用户信息")
    @GetMapping("/getUserInfoVo/{userId}")
    public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId) {
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
        return Result.ok(userInfoVo);
    }

    /**
     * 判断用户是否购买声音列表
     *
     * @param albumId
     * @param trackNeedPaidIdList
     * @return
     */
    @TsLogin(required = false)
    @Operation(summary = "判断用户是否购买声音列表")
    @PostMapping("/userIsPaidTrack/{albumId}")
    public Result<Map<Long, Integer>> userIsPaidTrack(@PathVariable Long albumId, @RequestBody List<Long> trackNeedPaidIdList) {
        Long userId = AuthContextHolder.getUserId();
        Map<Long, Integer> userIsPaidTrackMap = userInfoService.getUserIsPaidTrack(userId, albumId, trackNeedPaidIdList);
        return Result.ok(userIsPaidTrackMap);
    }

    /**
     * 判断用户是否购买专辑
     *
     * @param trackId
     * @return
     */
    @TsLogin
    @GetMapping("/isPaidAlbum/{albumId}")
    Result<Boolean> isPaidAlbum(@PathVariable("albumId") Long albumId) {

        Long userId = AuthContextHolder.getUserId();
        boolean flag = userInfoService.isPaidAlbum(userId, albumId);
        return Result.ok(flag);
    }

    /**
     * 根据专辑id获取用户支付过的声音id列表
     * @param albumId
     * @return
     */
    @TsLogin
    @Operation(summary = "根据专辑id获取用户支付过的声音id列表")
    @GetMapping("/findUserPaidTrackList/{albumId}")
    Result<List<Long>> findUserPaidTrackList(@PathVariable Long albumId){
        Long userId = AuthContextHolder.getUserId();
        List<Long> userPaidTrackList=userInfoService.findUserPaidTrackList(userId,albumId);
        return Result.ok(userPaidTrackList);
    }

    /**
     * 处理用户购买记录 保存用户购买交易记录; user_paid_album user_paid_track user_vip_service
     * @param userPaidRecordVo
     * @return
     */
    @PostMapping("/savePaidRecord")
    Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo){
        userInfoService.savePaidRecord(userPaidRecordVo);
        return Result.ok();
    }
}

